﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.Core.BaseStruct;
using System.Data.Common;

namespace Kugar.Core.Data
{
    public interface IKugarConnection : IRecyclable
    {
        DbConnection Connection { get; }

        string ConnectionString { get; }

        ExceptionHandleType ExceptionHandleType { set; get; }

        DbCommand CreateCommand();
        DbTransaction BeginTransaction();
        void RollbackTransaction();
        void CommitTransaction();


        #region "新的操作函数"
        //ResultReturn<int> ExecuteNoQuery(string sqlStr);
        //ResultReturn<int> ExecuteNoQuery(string sqlStr, params object[] param);
        //ResultReturn<int> ExecuteNoQuery(string sqlStr, ExceptionHandleType errorHandle, params object[] param);
        //ResultReturn<int> ExecuteNoQuery(DbCommand cmd);
        //ResultReturn<int> ExecuteNoQuery(DbCommand cmd, ExceptionHandleType errorHandle);

        //ResultReturn ExecuteScalar(string sqlStr);
        //ResultReturn ExecuteScalar(string sqlStr, params object[] param);
        //ResultReturn ExecuteScalar(string sqlStr, ExceptionHandleType errorHandle, params object[] param);
        //ResultReturn ExecuteScalar(DbCommand cmd);
        //ResultReturn ExecuteScalar(DbCommand cmd, ExceptionHandleType errorHandle);

        //object ExecuteScalar(string sqlStr);
        //object ExecuteScalar(string sqlStr, params object[] param);
        //object ExecuteScalar(string sqlStr, bool? isThrowError, params object[] param)
        //object ExecuteScalar(DbCommand cmd);
        //object ExecuteScalar(DbCommand cmd, bool? isThrowError);

        //T ExecuteScalar<T>(string sqlStr);
        //T ExecuteScalar<T>(string sqlStr, params object[] param);
        //T ExecuteScalar<T>(string sqlStr, bool? isThrowError, params object[] param)
        //T ExecuteScalar<T>(DbCommand cmd);
        //T ExecuteScalar<T>(DbCommand cmd, bool? isThrowError);

        //DbDataReader ExecuteReader(string sqlStr);
        //DbDataReader ExecuteReader(string sqlStr, params object[] param);
        //DbDataReader ExecuteReader(string sqlStr, bool? isThrowError, params object[] param)
        //DbDataReader ExecuteReader(DbCommand cmd);
        //DbDataReader ExecuteReader(DbCommand cmd, bool? isThrowError);


        //ResultReturn<DataTable> ExecuteDataTable(string sqlStr);
        //ResultReturn<DataTable> ExecuteDataTable(string sqlStr, params object[] param);
        //ResultReturn<DataTable> ExecuteDataTable(string sqlStr, ExceptionHandleType errorHandle, params object[] param);
        //ResultReturn<DataTable> ExecuteDataTable(DbCommand cmd);
        //ResultReturn<DataTable> ExecuteDataTable(DbCommand cmd, ExceptionHandleType errorHandle);
        //bool ExecuteDataTable(DbCommand cmd, DataTable tbl);
        //bool ExecuteDataTable(DbCommand cmd, DataTable tbl, ExceptionHandleType errorHandle);


        //ResultReturn<DataSet> ExecuteDataSet(string sqlStr);
        //ResultReturn<DataSet> ExecuteDataSet(string sqlStr, params object[] param);
        //ResultReturn<DataSet> ExecuteDataSet(string sqlStr, ExceptionHandleType errorHandle, params object[] param);
        //ResultReturn<DataSet> ExecuteDataSet(DbCommand cmd);
        //ResultReturn<DataSet> ExecuteDataSet(DbCommand cmd, ExceptionHandleType errorHandle);
        //void ExecuteDataSet(DbCommand cmd, DataSet ds);
        //void ExecuteDataSet(DbCommand cmd, DataSet ds, ExceptionHandleType errorHandle);


        #endregion

        #region "老的接口,已取消"

        int ExecuteNoQuery(string sqlStr);        
        int ExecuteNoQuery(string sqlStr, params object[] param);
        int ExecuteNoQuery(string sqlStr, bool? isThrowError, params object[] param);
        int ExecuteNoQuery(DbCommand cmd);
        int ExecuteNoQuery(DbCommand cmd, bool? isThrowError);

        object ExecuteScalar(string sqlStr);
        object ExecuteScalar(string sqlStr, params object[] param);
        object ExecuteScalar(string sqlStr, bool? isThrowError, params object[] param);
        object ExecuteScalar(DbCommand cmd);
        object ExecuteScalar(DbCommand cmd, bool? isThrowError);

        T ExecuteScalar<T>(string sqlStr);
        T ExecuteScalar<T>(string sqlStr, params object[] param);
        T ExecuteScalar<T>(string sqlStr, bool? isThrowError, params object[] param);
        T ExecuteScalar<T>(DbCommand cmd);
        T ExecuteScalar<T>(DbCommand cmd, bool? isThrowError);

        DbDataReader ExecuteReader(string sqlStr);
        DbDataReader ExecuteReader(string sqlStr, params object[] param);
        DbDataReader ExecuteReader(string sqlStr, bool? isThrowError, params object[] param);
        DbDataReader ExecuteReader(DbCommand cmd);
        DbDataReader ExecuteReader(DbCommand cmd, bool? isThrowError);

        Dictionary<string, object> ExecuteSingleRow(string sqlStr);
        Dictionary<string, object> ExecuteSingleRow(string sqlStr, params object[] param);
        Dictionary<string, object> ExecuteSingleRow(string sqlStr, bool? isThrowError, params object[] param);
        Dictionary<string,object>  ExecuteSingleRow(DbCommand cmd);
        Dictionary<string, object> ExecuteSingleRow(DbCommand cmd, bool? isThrowError);


        DataTable ExecuteDataTable(string sqlStr);
        DataTable ExecuteDataTable(string sqlStr, params object[] param);
        DataTable ExecuteDataTable(string sqlStr, bool? isThrowError, params object[] param);
        DataTable ExecuteDataTable(DbCommand cmd);
        DataTable ExecuteDataTable(DbCommand cmd, bool? isThrowError);
        void ExecuteDataTable(DbCommand cmd,DataTable tbl);
        void ExecuteDataTable(DbCommand cmd, DataTable tbl, bool? isThrowError);

        #endregion



        DataTable GetDatabaseTableSchema(string srcTableName,SchemaType schemaType);

        DataSet GetDatabaseAllTableSchema(SchemaType schemaType);

        string[] GetAllTableNames();

        IEnumerable<T> ExecuteDataEntity<T>(DbCommand cmd, Action<T, DbDataReader> afterPreRowBuild = null,
                                            Action<IEnumerable<T>, DbDataReader> afterAllRowBuild = null,bool? isThrowError=null)
            where T : class, new();

        IEnumerable<T> ExecuteDataEntity<T>(string sqlStr, Action<T, DbDataReader> afterPreRowBuild = null,
                                            Action<IEnumerable<T>, DbDataReader> afterAllRowBuild = null,bool? isThrowError=null,
                                            params object[] param) where T : class, new();

        IEnumerable<T> ExecuteDataEntity<T>(string sqlStr, Action<T, DbDataReader> afterPreRowBuild = null,
                                            Action<IEnumerable<T>, DbDataReader> afterAllRowBuild = null)
            where T : class, new();



        VM_PagedDataTable ExecutePagingDataTable(PagingArgs args,bool? isThrowError=null);
        VM_PagedDataReader ExecutePagingDataReader(PagingArgs args,bool? isThrowError=null);

        VM_PagedObjectList<T> ExecutePagingDataEntity<T>(PagingArgs args,
                                                                Action<T, DbDataReader> afterPreRowBuild = null,
                                                                Action<IEnumerable<T>, DbDataReader> afterAllRowBuild =
                                                                    null,bool? isThrowError=null) where T : class ,new();
    }

    public interface IKugarConnection2 : IRecyclable
    { 
        
    }

    [Flags]
    public enum ExceptionHandleType
    {
        Auto,
        ThrowException=1,
        LogException=2
        
    }
}
